using System;
using System.Collections;
using UnityEngine;


namespace RGuang.PlathformCtrl
{
    public class PlayerInput : MonoBehaviour
    {
        [SerializeField] float jumpInputBufferTime = 0.5f;
        public bool HasJumpInputBuffer { get; set; }
        public bool Jump => inputs.Gameplay.Jump.WasPerformedThisFrame();
        public bool StopJump => inputs.Gameplay.Jump.WasReleasedThisFrame();
        public float AxisX => m_axes.x;
        public bool Move => AxisX != 0f;

        Vector2 m_axes => inputs.Gameplay.Axes.ReadValue<Vector2>();
        PlayerInputActions inputs;
        WaitForSeconds waitForJumpBufferTime;
        void Awake()
        {
            inputs = new PlayerInputActions();
            waitForJumpBufferTime = new WaitForSeconds(jumpInputBufferTime);
        }

        //private void OnGUI()
        //{
        //    Rect rect = new Rect(200, 200, 200, 200);
        //    string msg = $"跳跃缓冲：{HasJumpInputBuffer}";
        //    GUIStyle style = new GUIStyle
        //    {
        //        fontSize = 20,
        //        fontStyle = FontStyle.Bold,
        //    };
        //    GUI.Label(rect, msg, style);
        //}


        public void OnEnable()
        {
            inputs.Gameplay.Jump.canceled += delegate { HasJumpInputBuffer = false; };
        }

        public void EnableGameplayInputs()
        {
            inputs.Gameplay.Enable();
            Cursor.lockState = CursorLockMode.Locked;
        }

        public void DisableGameplayInputs()
        {
            inputs.Gameplay.Disable();
        }
        public void SetJumpInputBufferTimer()
        {
            StopCoroutine(nameof(JumpInputBufferCoroutine));
            StartCoroutine(nameof(JumpInputBufferCoroutine));
        }
        IEnumerator JumpInputBufferCoroutine()
        {
            HasJumpInputBuffer = true;
            yield return waitForJumpBufferTime;
            HasJumpInputBuffer = false;
        }

    }


}
